const app = getApp()
Page({
  data: {
    topicId: '',
    ktxq: '',
    ffrom: '',
    appRea: ''
  },
  onLoad: function(options) {
    const that = this
    that.setData({
      topicId: options._id,
      ffrom: options.ffrom
    })
    if (options.ffrom == 'stuDs') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Topic').where({
        _id: options._id
      }).get().then(res => {
        that.setData({
          ktxq: res.data
        })
      })
    } else if (options.ffrom == 'stuWd') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Topic').where({
        _id: options._id
      }).get().then(res => {
        that.setData({
          ktxq: res.data
        })
      })
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Option').where({
        topicId: options._id,
        userId: app.globalData.userId
      }).get().then(res => {
        that.setData({
          appRea: res.data
        })
      })
    } else if (options.ffrom == 'stuWdkt') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Topic').where({
        _id: options._id
      }).get().then(res => {
        that.setData({
          ktxq: res.data
        })
      })
    }
  },
  sqxt: function() {
    const that = this
    if (app.globalData.userState == 0) {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Option').where({
        userId: app.globalData.userId
      }).count().then(res => {
        if (res.total >= app.globalData.stuKtmax) {
          wx.showToast({
            title: '发送申请失败，超过最大同时申请上限！',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.cloud.database({
            env: 'gtss-2b1812'
          }).collection('Option').where({
            userId: app.globalData.userId,
            topicId: that.data.topicId
          }).count().then(res => {
            if (res.total > 0) {
              wx.showToast({
                title: '发送申请失败，请勿重复发送选题申请！',
                icon: 'none',
                duration: 2000
              })
            } else {
              wx.navigateTo({
                url: '../stuRea/stuRea?topicId=' + that.data.topicId + '&topicName=' + that.data.ktxq[0].ktmc + '&teaId=' + that.data.ktxq[0].teaId
              })
            }
          })

        }
      })
    }
    if (app.globalData.userState == 1) {
      wx.showToast({
        title: '发送申请失败，您的课题已经确定！',
        icon: 'none',
        duration: 2000
      })
    }
  },
  chxtsq: function() {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确认继续撤回选题申请？',
      cancelColor: '#000000',
      cancelText: '确认',
      confirmColor: '#576B95',
      confirmText: '取消',
      success(res) {
        if (res.confirm) {} else if (res.cancel) {
          wx.cloud.callFunction({
            name: 'cancelTopicApply',
            data: {
              stuId: app.globalData.userId,
              stuName: app.globalData.userName,
              topicId: that.data.topicId,
              topicName: that.data.ktxq[0].ktmc,
              teaId: that.data.ktxq[0].teaId
            }
          }).then(res => {
            if (res.result.result == 1) {
              wx.showToast({
                title: '成功撤回选题申请！',
                icon: 'success',
                duration: 2000
              })
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 2500)
            } else {
              wx.showToast({
                title: '撤回选题申请失败！',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  },
  sqtt: function() {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确认申请退题？',
      cancelColor: '#000000',
      cancelText: '确认',
      confirmColor: '#576B95',
      confirmText: '取消',
      success(res) {
        if (res.confirm) {} else if (res.cancel) {
          wx.cloud.callFunction({
            name: 'dropTopic',
            data: {
              stuId: app.globalData.userId,
              stuName: app.globalData.userName,
              topicId: that.data.topicId,
              topicName: that.data.ktxq[0].ktmc,
              teaId: that.data.ktxq[0].teaId
            }
          }).then(res => {
            if (res.result.result == 1) {
              wx.showToast({
                title: '成功发送退题申请，请等待导师批准',
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: '发送退题申请失败！',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  }
})